//
//  WBWelcomeController.swift
//  weiboD
//
//  Created by gao2015 on 2016/12/4.
//  Copyright © 2016年 gao2015. All rights reserved.
//

import UIKit
import SDWebImage

class WBWelcomeController: UIViewController {
    
   private lazy var bgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))

    //用户头像
   private lazy var userImageView : UIImageView = {
    
        let imageView =  UIImageView(image: UIImage(named:"avatar_default_big"))
        if let imageUrl = WBUserAccoutViewModel.shareUserAccout.userAccount?.avatar_large{
            imageView.sd_setImage(with: URL(string: imageUrl), placeholderImage: UIImage(named: "avatar_default_big"))
        }
            imageView.layer.cornerRadius = 45
            imageView.layer.masksToBounds = true
            return imageView
        
        }()
    
    private lazy var messageLabel : UILabel = {
        let Label = UILabel()
        if let name = WBUserAccoutViewModel.shareUserAccout.userAccount?.name{
            Label.text = "欢迎回来,\(name)"
        
        }else{
            Label.text = "欢迎回来"
        }
        Label.font = UIFont.systemFont(ofSize: 13)
        Label.textColor = UIColor.darkGray
        //默认透明的label
        Label.alpha = 0
        return Label
    }()
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        startAnimation()
    }
    
    
    
    private func startAnimation()  {
        //更新约束
        userImageView.snp_updateConstraints { (make) in
            make.top.equalTo(view).offset(100)
        }
        
        UIView.animate(withDuration: 2, delay: 0.5, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: {
            self.view.layoutIfNeeded()
            
        }, completion: {(_) -> Void in
            UIView.animate(withDuration: 1, animations: {
                self.messageLabel.alpha = 1
            }, completion: { (_) in
                //这里表示欢迎页面加载完成了已经
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: SwitchRootViewControllerName), object: self)
            })
        })
    
    }
    
    override func loadView() {
        view = bgImageView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    func setupUI() {
        view.addSubview(userImageView)
        view.addSubview(messageLabel)
        
        userImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(300)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        
        messageLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(userImageView)
            make.top.equalTo(userImageView.snp_bottom).offset(10)
        }
        
    }
}
